#include <n32g45x_tim.h>
#include <tim.h>


void times_init(void)
{
    tim1_init();
    tim2_init();
    tim6_init();
    tim8_init();
}

void tim1_init(void)
{
    /* TIM1 clock enable */
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_TIM1, ENABLE);
    TIM_ConfigPrescaler(TIM1, 2, TIM_PSC_RELOAD_MODE_IMMEDIATE);

    NVIC_InitType NVIC_InitStructure;
    /* Enable the TIM1 global Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel                   = TIM1_UP_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    TIM_TimeBaseInitType TIM_TimeBaseStructure;
    /* Time base configuration */
    TIM_TimeBaseStructure.RepetCnt = 0;
    TIM_TimeBaseStructure.Prescaler = 144 - 1;
    TIM_TimeBaseStructure.Period    = 1000 - 1;
    TIM_TimeBaseStructure.ClkDiv    = 0;
    TIM_TimeBaseStructure.CntMode   = TIM_CNT_MODE_UP;
    TIM_InitTimeBase(TIM1, &TIM_TimeBaseStructure);

    /* TIM1 enable update irq */
    TIM_ConfigInt(TIM1, TIM_INT_UPDATE, ENABLE);
    TIM_ClrIntPendingBit(TIM1, TIM_INT_UPDATE);
    /* TIM1 enable counter */
    TIM_Enable(TIM1, ENABLE);

}

void tim2_init(void)
{
    /* TIM2 clock enable */
    RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM2, ENABLE);
    TIM_ConfigPrescaler(TIM2, 2, TIM_PSC_RELOAD_MODE_IMMEDIATE);

    TIM_TimeBaseInitType TIM_TimeBaseStructure;
    /* Time base configuration */
    TIM_TimeBaseStructure.RepetCnt = 0;
    TIM_TimeBaseStructure.Prescaler = 72 - 1;	//7199
    TIM_TimeBaseStructure.Period    = 1000 - 1;
    TIM_TimeBaseStructure.ClkDiv    = 0;
    TIM_TimeBaseStructure.CntMode   = TIM_CNT_MODE_UP;
    TIM_InitTimeBase(TIM2, &TIM_TimeBaseStructure);

    OCInitType TIM_OCInitStructure;
    /* Channel 1 Configuration in PWM mode */
    TIM_OCInitStructure.OcMode       = TIM_OCMODE_PWM1;
    TIM_OCInitStructure.OutputState  = TIM_OUTPUT_STATE_ENABLE;
    TIM_OCInitStructure.Pulse        = 1;
    TIM_OCInitStructure.OcPolarity   = TIM_OC_POLARITY_LOW;

    TIM_InitOc2(TIM2, &TIM_OCInitStructure);
    /* TIM1 enable counter */
    TIM_Enable(TIM2, ENABLE);

    TIM_EnableCtrlPwmOutputs(TIM2, ENABLE);
}
void tim6_init(void)
{
    /* TIM6 clock enable */
    RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM6, ENABLE);
    TIM_ConfigPrescaler(TIM6, 2, TIM_PSC_RELOAD_MODE_IMMEDIATE);

    NVIC_InitType NVIC_InitStructure;
    /* Enable the TIM6 global Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel                   = TIM6_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    TIM_TimeBaseInitType TIM_TimeBaseStructure;
    /* Time base configuration */
    TIM_TimeBaseStructure.RepetCnt = 0;
    TIM_TimeBaseStructure.Prescaler = 7200 - 1;	//7199
    TIM_TimeBaseStructure.Period    = 10000 - 1;
    TIM_TimeBaseStructure.ClkDiv    = 0;
    TIM_TimeBaseStructure.CntMode   = TIM_CNT_MODE_UP;
    TIM_InitTimeBase(TIM6, &TIM_TimeBaseStructure);

    /* TIM6 enable update irq */
    TIM_ConfigInt(TIM6, TIM_INT_UPDATE, ENABLE);
    TIM_ClrIntPendingBit(TIM6, TIM_INT_UPDATE);
    /* TIM6 enable counter */
    TIM_Enable(TIM6, ENABLE);
}

void tim8_init(void)
{
    /* TIM8 clock enable */
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_TIM8, ENABLE);
    TIM_ConfigPrescaler(TIM8, 2, TIM_PSC_RELOAD_MODE_IMMEDIATE);

    TIM_TimeBaseInitType TIM_TimeBaseStructure;
    /* Time base configuration */
    TIM_TimeBaseStructure.RepetCnt = 0;
    TIM_TimeBaseStructure.Prescaler = 144 - 1;		//71
    TIM_TimeBaseStructure.Period    = 65535;
    TIM_TimeBaseStructure.ClkDiv    = 0;
    TIM_TimeBaseStructure.CntMode   = TIM_CNT_MODE_UP;
    TIM_InitTimeBase(TIM8, &TIM_TimeBaseStructure);

//    /* TIM8 enable update irq */
//    TIM_ConfigInt(TIM8, TIM_INT_UPDATE, ENABLE);

//    /* TIM8 enable counter */
//    TIM_Enable(TIM8, ENABLE);
}